package com.tianyadu.apigateway.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.POST_TYPE;
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.SEND_RESPONSE_FILTER_ORDER;

/**
 * @FileName: addResponseHeaderFilter
 * @Description: post过滤器自定义（添加返回参数信息）
 * @author: xueyj
 * @create: 2018-11-26 18:12
 */
@Component
public class addResponseHeaderFilter extends ZuulFilter {

    /**
      * @Description: 拦截类型：post
      * @param:
      * @return:
      * @author:
      * @date:   2018/11/26 18:13
      */
    @Override
    public String filterType() {
        return POST_TYPE;
    }

    /**
      * @Description: 拦截级别
      * @param:
      * @return:
      * @author:
      * @date:   2018/11/26 18:14
      */
    @Override
    public int filterOrder() {
        return SEND_RESPONSE_FILTER_ORDER - 1;
    }

    /**
      * @Description: 是否开启过滤
      * @param:
      * @return:
      * @author:
      * @date:   2018/11/26 18:14
      */
    @Override
    public boolean shouldFilter() {
        return true;
    }

    /**
      * @Description: 拦截信息处理
      * @param:
      * @return:
      * @author:
      * @date:   2018/11/26 18:14
      */
    @Override
    public Object run() {
        RequestContext requestContext = RequestContext.getCurrentContext();
        HttpServletResponse response = requestContext.getResponse();
        response.setHeader("X-Foo", UUID.randomUUID().toString());
        return null;
    }
}